package com.kudian.restful.service;

import com.kudian.common.persistence.HibernateDao;
import com.kudian.common.service.BaseService;
import com.kudian.common.utils.DateUtils;
import com.kudian.common.utils.MessageUtils;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.dao.*;
import com.kudian.restful.entity.*;
import com.kudian.restful.vo.ad.AdItemVO;
import com.kudian.restful.vo.ad.MidAdRetVO;
import com.kudian.restful.vo.ad.MidAdVO;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/18.
 */
@Service
@Transactional(readOnly = true)
public class PayLogService extends BaseService<PayLog, Integer> {
    private static final Logger logger = Logger.getLogger(PayLogService.class);

    @Autowired
    private OrderInfoDao orderInfoDao;

    @Autowired
    private OrderGoodsDao orderGoodsDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private SellerDao sellerDao;

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private PayLogDao payLogDao;

    @Override
    public HibernateDao<PayLog, Integer> getEntityDao() {
        return payLogDao;
    }

    @Transactional(readOnly = false)
    public void savePayLog(String outTradeNo) {
        logger.info("savePayLog==>" + outTradeNo);
        PayLog payLog = new PayLog();
        List<Integer> orderIds = new ArrayList<Integer>();
        List<PayLog> pls = payLogDao.find(Restrictions.eq("outTradeNo", outTradeNo));
        for (PayLog pl : pls) {
            if (pl.getIsPaid() == (short)1) {
                return;
            }

            pl.setIsPaid((short) 1);
            this.payLogDao.save(pl);
            orderIds.add(pl.getOrderId());
        }

        if (!orderIds.isEmpty()) {
            List<OrderInfo> ois = orderInfoDao.find(Restrictions.in("orderId", orderIds));
            for (OrderInfo oi : ois) {
                oi.setPayStatus((short) 1);
                oi.setPayTime(DateUtils.getSysTimestamp());
                this.orderInfoDao.save(oi);

                // 增加推送商家 // 扣款成功先推送消息,在减库存
                Seller s = this.sellerDao.get(oi.getSellerId());
                Users ub = this.usersDao.get(s.getUserId());
                if (StringUtils.isNotBlank(ub.getSRegistrationId())) {
                    String msg = oi.getOrderId() + "," + oi.getOrderSn();
                    logger.info("msg=" + msg);
                    MessageUtils.sendmsg2bus(msg, ub.getSRegistrationId());
                }
            }
        }

        // 支付完成可以扣除库存
        if (!orderIds.isEmpty()) {
            List<OrderGoods> ogs = orderGoodsDao.find(Restrictions.in("orderId", orderIds));
            for (OrderGoods og : ogs) {
                Integer goodsId = og.getGoodsId();
                Short goodsNumber = og.getGoodsNumber();

                // 支付成功,减库存
                Goods goods = goodsDao.get(goodsId);
                if (goods != null) {
                    Integer inventory = goods.getInventory();
                    if (inventory != null && inventory.intValue() > 0) {
                        inventory = inventory - goodsNumber;
                        if (inventory.intValue() < 0) {
                            goods.setInventory(0);
                        } else {
                            goods.setInventory(inventory);
                        }
                        this.goodsDao.save(goods);
                    }
                }

            }
        }
    }

    @Transactional(readOnly = false)
    public void addPayLog(String outTradeNo, List<Integer> odrderIds, Short orderChannel) {
        for (Integer orderId : odrderIds) {
            PayLog payLog = new PayLog();
            payLog.setOrderId(orderId);
            OrderInfo oi = orderInfoDao.get(orderId);
            if (oi == null) {
                continue;
            }

            payLog.setOrderAmount(oi.getOrderAmount());
            payLog.setOrderChannel(orderChannel);
            payLog.setOrderType((short) 0);
            payLog.setOutTradeNo(outTradeNo);
            payLog.setIsPaid((short) 0);
            payLog.setAddTime(DateUtils.getSysTimestamp());
            payLogDao.save(payLog);
        }
    }
}
